exports.__esModule = true;

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _domain = require('domain');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

const unlink = think.promisify(_fs2.default.unlink, _fs2.default);

exports.default = class extends think.Service {
  constructor() {
    super();
    this.eventGroupModel = this.db['cms_event_group'];
    this.eventModel = this.db['cms_event'];
    this.cellModel = this.db['cms_cell'];
    this.areaModel = this.db['cms_area'];
    this.attachmentModel = this.db['sys_attachment'];
    this.userModel = this.db['sys_user'];
    this.eventModel.belongsTo(this.eventGroupModel, {
      foreignKey: 'group_code',
      targetKey: 'group_code'
    });
    this.eventModel.belongsTo(this.cellModel, {
      foreignKey: 'BGID',
      targetKey: 'BGID'
    });
    this.eventModel.belongsTo(this.areaModel, {
      foreignKey: 'area_code',
      targetKey: 'area_code'
    });
    this.eventModel.hasMany(this.attachmentModel, {
      foreignKey: 'businesskey',
      targetKey: 'id'
    });
    this.serverIP = 'http://' + think.config('staticIp') + ':' + think.config('proxyPort');
    this.wechatIp = 'http://xm219.top';
  }
  getAllEventGroup() {
    var _this = this;

    return _asyncToGenerator(function* () {
      try {
        return yield _this.eventGroupModel.findAll({
          where: {
            del_flag: 0
          }
        });
      } catch (err) {
        return _this.exceptionService.handleError(err);
      }
    })();
  }

  /**
     * 按查询条件获取事件列表
     */
  getEventListBy(data) {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      data.start = (parseInt(data.page) - 1) * parseInt(data.pageSize);
      const Op = _this2.db.Sequelize.Op;
      const is_check = data.is_check.map(function (numStr) {
        return Number(numStr);
      });
      const where = {
        del_flag: 0,
        is_check: {
          [Op.in]: is_check
        }
      };
      if (Object.is(data.flag, 0)) {
        where.create_by = data.userId;
      } else if (Object.is(data.flag, 1)) {
        where.verify_by = data.userId;
      }
      if (data.group_code !== '' && data.area_code !== undefined) {
        where.group_code = data.group_code;
      }
      if (data.area_code !== '' && data.area_code !== undefined) {
        where.area_code = data.area_code;
      }

      const rows = [];
      try {
        const list = yield _this2.eventModel.findAndCountAll({
          where: where,
          include: [{
            model: _this2.eventGroupModel,
            attributes: ['group_name']
          }, {
            model: _this2.cellModel,
            attributes: ['scope_path']
          }, {
            model: _this2.areaModel,
            attributes: ['area_code', 'area_name']
          }, {
            model: _this2.attachmentModel,
            attributes: ['realpath']
          }],
          limit: parseInt(data.pageSize),
          offset: parseInt(data.start),
          order: [['is_check', 'ASC'], ['create_date', 'DESC']],
          distinct: true
        });
        if (list.rows.length > 0) {
          for (let i = 0; i < list.rows.length; i++) {
            const item = list.rows[i].dataValues;
            const sys_attachment = [];
            if (item.sys_attachments.length > 0) {
              for (let j = 0; j < item.sys_attachments.length; j++) {
                sys_attachment.push(_this2.serverIP + '/' + item.sys_attachments[j].realpath);
              }
            }
            item.sys_attachments = sys_attachment;
            rows.push(item);
          }
        }
        list.rows = rows;
        return list;
      } catch (err) {
        return _this2.exceptionService.handleError(err);
      }
    })();
  }

  /**
     * 新增事件
     * @param {Object} data 事件属性
     */
  addEvent(data) {
    var _this3 = this;

    return _asyncToGenerator(function* () {
      data.id = think.uuid('v1');
      const Op = _this3.db.Sequelize.Op;
      try {
        if (data.pic_path) {
          // const where = {
          //     realpath:{
          //         [Op.in]:data.pic_path
          //     }
          // };
          // const updateField = {
          //     businesskey:data.id
          // };
          // await this.attachmentModel.update(updateField,{where:where});

          const fileArr = data.pic_path;
          const insertData = fileArr.map(function (item) {
            const param = {
              id: think.uuid('v1'),
              create_date: new Date(),
              businesskey: data.id,
              realpath: item,
              user_id: data.create_by
            };
            return param;
          });
          yield _this3.attachmentModel.bulkCreate(insertData);
        }
        const event = yield _this3.eventModel.create(data);
        return JSON.parse(JSON.stringify(event));
      } catch (err) {
        return _this3.exceptionService.handleError(err);
      }
    })();
  }
  /**
     * 修改事件
     * @param {Object} data 事件属性
     */
  editEvent(data) {
    var _this4 = this;

    return _asyncToGenerator(function* () {
      const attrachWhere = {
        businesskey: data.id
      };
      try {
        const old = yield _this4.attachmentModel.findAll({
          where: attrachWhere,
          attributes: ['realpath']
        });
        const fileArr = old.map(function (path) {
          return path.realpath;
        });
        if (data.pic_path) {
          const newPic = data.pic_path;
          const oldPic = old.map(function (path) {
            return path.realpath;
          });
          const delORadd = yield _this4.updateAttrachment(oldPic, newPic, data.id, data.update_by);
          if (delORadd.error) {
            return {
              msg: '附件信息更新失败'
            };
          }
        } else {
          yield _this4.attachmentModel.destroy({
            where: attrachWhere
          });
          const res = yield _this4.delFile(fileArr);
          if (!res) {
            return {
              msg: '事件相关文件删除失败'
            };
          }
        }
        return yield _this4.eventModel.update(data, {
          where: {
            id: data.id
          }
        });
      } catch (err) {
        return _this4.exceptionService.handleError(err);
      }
    })();
  }

  /**
     * 更新附件表删除图片
     * oldPic --原来的图片名称列表
     * newPic --新的图片名称列表
     * eventid -- 事件id
     * userid -- 用户id
     */
  updateAttrachment(oldPic, newPic, eventid, userid) {
    var _this5 = this;

    return _asyncToGenerator(function* () {
      let flag1 = true;
      let flag2 = true;
      let addList = [];
      let delList = [];
      const Op = _this5.db.Sequelize.Op;
      const ar = oldPic.filter(function (a) {
        // 重复部分
        return newPic.indexOf(a) != -1;
      });
      addList = newPic.filter(function (b) {
        return ar.indexOf(b) === -1;
      });
      delList = oldPic.filter(function (c) {
        return ar.indexOf(c) === -1;
      });
      try {
        if (addList.length > 0) {
          addList = addList.map(function (item) {
            const addData = {
              id: think.uuid('v1'),
              realpath: item,
              create_date: new Date(),
              atta_title: '事件上报',
              businesskey: eventid,
              user_id: userid
            };
            return addData;
          });
          const add = yield _this5.attachmentModel.bulkCreate(addList);
          if (!add) {
            flag1 = false;
          }
        }
        if (delList.length > 0) {
          const fileArr = delList.map(function (path) {
            return path;
          });
          const del = yield _this5.attachmentModel.destroy({
            where: {
              businesskey: eventid,
              realpath: {
                [Op.in]: delList
              }
            }
          });
          if (!del) {
            flag2 = false;
          } else {
            const delImg = yield _this5.delFile(fileArr);
            if (!delImg) {
              flag2 = false;
            }
          }
        }
        if (flag1 && flag1) {
          return true;
        } else {
          return {
            error: '操作错误'
          };
        }
      } catch (err) {
        return _this5.exceptionService.handleError(err);
      }
    })();
  }

  /**
     * 删除事件
     * @param {*} data
     * data.id --事件id
     */
  delEvent(data) {
    var _this6 = this;

    return _asyncToGenerator(function* () {
      const upfields = {
        del_flag: 1
      };
      try {
        return _this6.eventModel.update(upfields, {
          where: data
        });
      } catch (err) {
        return _this6.exceptionService.handleError(err);
      }
    })();
  }

  /**
     * 核实事件
     * data.is_check -- 是否核实 0否 1是
     * data.id -- 事件id
     */
  checkEvent(data) {
    var _this7 = this;

    return _asyncToGenerator(function* () {
      const upfield = data;
      const where = {
        id: data.id
      };
      try {
        return yield _this7.eventModel.update(upfield, {
          where: where
        });
      } catch (err) {
        return _this7.exceptionService.handleError(err);
      }
    })();
  }

  /**
     * 返回核实事件的详情
     */
  checkEventDetail(data) {
    var _this8 = this;

    return _asyncToGenerator(function* () {
      const where = {
        id: data.id
      };
      try {
        return yield _this8.eventModel.findOne({
          where: where
        });
      } catch (err) {
        return _this8.exceptionService.handleError(err);
      }
    })();
  }

  /**
     * 事件详情
     * data.id --事件id
     */
  getEventInfo(data) {
    var _this9 = this;

    return _asyncToGenerator(function* () {
      try {
        const sql = `SELECT a.*, b.username AS create_name, c.username AS update_name, d.username AS verify_name, b.pic_path AS create_pic, c.pic_path AS update_pic, d.pic_path AS verify_pic,e.area_name FROM cms_event a LEFT JOIN sys_user b ON (b.id = a.create_by) LEFT JOIN sys_user c ON (c.id = a.update_by) LEFT JOIN sys_user d ON (d.id = a.verify_by) LEFT JOIN cms_area e ON (e.area_code = a.area_code) WHERE a.id = '${data.id}';`;
        const eventInfo = yield _this9.db.sequelize.query(sql, {
          type: _this9.db.sequelize.QueryTypes.SELECT
        });
        if (eventInfo.length > 0) {
          const res = eventInfo[0];
          res.create_pic = yield _this9.checkPic(res.create_pic);
          res.update_pic = yield _this9.checkPic(res.update_pic);
          res.verify_pic = yield _this9.checkPic(res.verify_pic);
          let pic_path = yield _this9.attachmentModel.findAll({
            where: {
              businesskey: data.id
            },
            attributes: ['realpath']
          });
          if (pic_path.length > 0) {
            pic_path = pic_path.map(function (item) {
              return _this9.serverIP + '/' + item.realpath;
            });
          }
          if (res.undefined) {
            delete res.undefined;
          }
          res.pic_path = pic_path;
          return res;
        } else {
          return {
            error: '获取事件信息错误'
          };
        }
      } catch (err) {
        return _this9.exceptionService.handleError(err);
      }
    })();
  }

  getWechatEventInfo(data) {
    var _this10 = this;

    return _asyncToGenerator(function* () {
      try {
        const sql = `SELECT a.*, b.NickName AS create_name, c.username AS update_name, d.username AS verify_name,
             b.HeadPic AS create_pic, c.pic_path AS update_pic, d.pic_path AS verify_pic,e.area_name FROM cms_event a 
             LEFT JOIN wechat_user b ON (b.UserId = a.create_by) 
             LEFT JOIN sys_user c ON (c.id = a.update_by) 
             LEFT JOIN sys_user d ON (d.id = a.verify_by) 
             LEFT JOIN cms_area e ON (e.area_code = a.area_code) WHERE a.id = '${data.id}';`;
        const eventInfo = yield _this10.db.sequelize.query(sql, {
          type: _this10.db.sequelize.QueryTypes.SELECT
        });
        if (eventInfo.length > 0) {
          const res = eventInfo[0];
          res.create_pic = _this10.serverIP + '/static/upload/images/default.png';
          res.update_pic = yield _this10.checkPic(res.update_pic);
          res.verify_pic = yield _this10.checkPic(res.verify_pic);
          let pic_path = yield _this10.attachmentModel.findAll({
            where: {
              businesskey: data.id
            },
            attributes: ['realpath']
          });
          if (pic_path.length > 0) {
            pic_path = pic_path.map(function (item) {
              return _this10.wechatIp + '/' + item.realpath;
            });
          }
          if (res.undefined) {
            delete res.undefined;
          }
          res.pic_path = pic_path;
          return res;
        } else {
          return {
            error: '获取事件信息错误'
          };
        }
      } catch (err) {
        return _this10.exceptionService.handleError(err);
      }
    })();
  }

  /**
     *
     */
  verifyStateList() {
    var _this11 = this;

    return _asyncToGenerator(function* () {
      const sql = `select a.typecode as check_code,a.typename as check_name from sys_type a 
        LEFT JOIN sys_type_group b on (b.id=a.typegroupid) where b.typegroupcode='sjzt'`;
      try {
        return yield _this11.db.sequelize.query(sql, {
          type: _this11.db.sequelize.QueryTypes.SELECT
        });
      } catch (err) {
        return _this11.exceptionService.handleError(err);
      }
    })();
  }
  /**
     * 组装图片路径
     */
  checkPic(path) {
    var _this12 = this;

    return _asyncToGenerator(function* () {
      let data = _this12.serverIP + '/static/upload/images/default.png';
      if (path) {
        data = _this12.serverIP + '/' + path;
      }
      return data;
    })();
  }
  /**
     * 删除文件
     * @returns Boolean
     */
  delFile(filePathArr) {
    return _asyncToGenerator(function* () {
      try {
        for (var i in filePathArr) {
          yield unlink('www/' + filePathArr[i]);
        }
        return true;
      } catch (e) {
        return false;
      }
    })();
  }
};